package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.query.TravelReplyQueryObject;
import cn.wolfcode.trip.base.service.IGreatService;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelReplyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author by mycp
 * @Classname TravelController
 * @Description TODO 游记资源处理器
 * @Date 2018/11/5 9:30
 */
@RestController
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    @Autowired
    private ITravelCommendService travelCommendService;

    @Autowired
    private IGreatService greatService;

    @Autowired
    private ITravelReplyService travelReplyService;

    /**
     * 查询指定的游记信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Object getTravelById(@PathVariable Long id){
        Travel travel = travelService.get(id);
        return travel;
    }

    /**
     * 查询指定作者的游记数量
     * @param authorId 作者id
     * @return
     */
    @GetMapping("/{authorId}/numbers")
    public Object count(@PathVariable Long authorId){
        Integer count = travelService.count(authorId);
        return count;
    }

    /**
     * 接口: /travels
     * 动作: GET
     * 参数: isPublic = true & state = 2
     * 获取全部以发布的游记信息
     * @return
     */
    @GetMapping
    public Object getTravels(TravelQueryObject qo){
        // 按照发布游记时间最近降序排序
        qo.setOrderBy("t.releaseTime desc");
        return travelService.queryTravel(qo);
    }


    /**
     * 接口: /travels/commends
     * 动作: GET
     * 参数: Integer type 推荐游记类型
     * 获取所有指定推荐类型的游记
     * @param type
     * @return
     */
    @GetMapping("/commends")
    public Object getTravelCommends(@RequestParam Integer type){
        List<TravelCommend> list = travelCommendService.listCommends(type);
        return list;
    }

    /**
     * 接口: /travels/{id}/greats
     * 动作: GET
     * 参数: greatType="travel_id"
     * 获取指定游记的点赞数
     */
    @GetMapping("/{greatTypeId}/greats")
    public Object getTravelGreats(String greatType,@PathVariable Long greatTypeId){
        Map<String, Object> map = new HashMap<>();
        Integer greats = greatService.queryForTotalCount(greatType,greatTypeId);
        map.put("greats", greats);
        return map;
    }

    /**
     * 接口:/travels/{id}/greats
     * 动作:post
     * 参数:greatType="travel_id"&userId=xxx
     */
    @PostMapping("/{greatTypeId}/greats")
    public Object great(String greatType,@PathVariable Long greatTypeId, Great entity){
        // 定义返回的数据
        Map<String, Object> map = new HashMap<>();
        // 查询是否有该用户的点赞记录
        Great great = greatService.selectGreatByGreatTypeIdAndUserId(greatType,greatTypeId, entity.getUserId());
        if (great != null) {
            // 如果找到了这条记录, 则删除掉这条点赞
            greatService.removeGreatByGreatTypeIdAndUserId(greatType,greatTypeId, entity.getUserId());
        } else {
            // 如果找不到这条记录, 则添加一条点赞数
            greatService.saveGreatByGreatTypeIdAndUserId(greatType,greatTypeId, entity);
        }
        // 重新查询游记点赞数
        Integer greats = greatService.queryForTotalCount(greatType,greatTypeId);
        // 设置响应数据
        map.put("greats", greats);
        return map;
    }

    /**
     * 接口: 获取指定游记id的评论
     * /travels/{id}/replies
     * 动作: GET
     * 参数:state=1&pageSize=2&currentPage=x
     */
    @GetMapping("/{travelId}/replies")
    public Object getReplyByTravelId(TravelReplyQueryObject qo){
        qo.setOrderBy("tr.createTime");
        PageInfo pageInfo = travelReplyService.query(qo);
        return pageInfo;
    }

    /**
     * 接口: 保存指定游记id的评论
     * /travels/{id}/replies
     * 动作: POST
     * 参数:content=x
     */
    @PostMapping("/{travelId}/replies")
    public Object saveReply(TravelReply entity){
        JsonResult jsonResult = new JsonResult();
        //设置评论人
        entity.setReplier(UserContext.getCurrentUser());
        try {
            travelReplyService.saveReply(entity);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("服务器繁忙, 请稍后再试");
        }
        return jsonResult;
    }

    /**
     * 接口: 获取指定游记id的评论数
     * /travels/{id}/replyCount
     * 动作: GET
     * 参数:
     */
    @GetMapping("/{travelId}/replyCount")
    public Object getReplyCount(@PathVariable Long travelId) {
        HashMap<String, Object> map = new HashMap<>();
        int replyCount = travelReplyService.getCountByTravelId(travelId);
        map.put("replyCount",replyCount);
        return map;
    }

}
